package org.pay;

import android.os.Handler;
import android.os.Message;

import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.pay.gate.model.PayGateRespModel;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public abstract class PayCallback {

    public static final int CODE_SUCCESS = 1;
    public static final int CODE_FAILURE = 0;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case CODE_FAILURE:
                    LogUtil.e("返回數據", "==========" + msg.obj);
                    onFailure((Exception) msg.obj);
                    break;
                case CODE_SUCCESS:
                    // 將簡體轉爲繁體
                    PayGateRespModel result = (PayGateRespModel) msg.obj;
                   /* String traditional=result;
                    try {
                        Map map = GsonUtils.JsonToBean(result, Map.class);
                         traditional = ZhConverterUtil.toTraditional(new Gson().toJson(map));
                    }catch (Exception e){
                         traditional=result;
                    }*/
                    LogUtil.e("返回數據（簡體）", "==========" + result);

                    onSuccess(result);
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 主線程調用
     *
     * @param
     */
    protected abstract void onSuccess(PayGateRespModel payRespData);

    public void onSuccess(Response response,PayGateRespModel payRespData){
        Message message = mHandler.obtainMessage();
        message.what = CODE_SUCCESS;
        message.obj = payRespData;
        mHandler.sendMessage(message);
    }

    /**
     * 主線程調用
     *
     * @param e
     */
    protected abstract void onFailure(Exception e);
    public void onFailure(Call call, Exception e ){
        Message message = mHandler.obtainMessage();
        message.what = CODE_FAILURE;
        message.obj = e;
        mHandler.sendMessage(message);
    }


}
